﻿using System.Runtime.CompilerServices;

namespace Letter.IO.Scheduler;

public sealed class PipelineSchedulerAllotter
{
    public PipelineSchedulerAllotter(int count)
    {
        this.schedulers = new APipelineScheduler[count];
        for (int i = 0; i < this.schedulers.Length; i++)
        {
            this.schedulers[i] = new PipelineSingleScheduler();
        }
    }
    
    private int schedulerIndex;
    private APipelineScheduler[] schedulers;

    public APipelineScheduler[] Schedulers
    {
        get { return this.schedulers; }
    }

    public int Count
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { return this.schedulers.Length; }
    }

    public APipelineScheduler Next()
    {
        var scheduler = this.schedulers[this.schedulerIndex];
        this.schedulerIndex = (this.schedulerIndex + 1) % this.Count;
        return scheduler;
    }

    public void Shutdown()
    {
        for (int i = 0; i < this.schedulers.Length; i++)
        {
            this.schedulers[i].Dispose();
        }
    }
}
